﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CIT.WMS.RM.Bll;

namespace CIT.WMS.RM
{
    public partial class FrmRMPRIn : Form
    {
        public FrmRMPRIn()
        {
            InitializeComponent();
        }

        DataTable DtIn;
        DataTable DtRMIn;
        string DetailNo = string.Empty;
        string productNumber = string.Empty;

        private void txt_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                try
                {
                    lab_msg.Text = "";
                    //根据扫描二维码拆分单号，行号，序号
                    var splitList = txt_BillNo.Text.Split('-');
                    if (splitList.Length == 3)
                    {
                        DetailNo = "1";
                    }
                    else
                    {
                        DetailNo = splitList[3].ToString();
                    }
                    var BillNo = splitList.FirstOrDefault();
                    txt_BillNo.Text = BillNo;
                    lblRow.Text = splitList[1].ToString();
                    lblOrder.Text = splitList[2].ToString();
                    //DetailNo = splitList[3].ToString();
                    DtRMIn = Bll_RMIn.GetRMTL(BillNo);
                    if (DtRMIn != null && DtRMIn.Rows.Count > 0)
                    {
                        lab_StorageSN.Text = DtRMIn.Rows[0]["Storage_SN"].ToString();
                    }
                    else
                    {
                        MessageBox.Show("请选择正确的单号!");
                        txt_BillNo.Text = "";
                        return;
                    }

                    //查询出入库数量
                    DtIn = Bll_RMIn.GetISRMDNum(txt_BillNo.Text, lblRow.Text, lblOrder.Text, DetailNo);

                    if (DtIn != null && DtIn.Rows.Count > 0)
                    {
                        if (DtIn.Rows[0]["Status"].ToString() == "9")
                        {
                            MessageBox.Show("该条码信息已入库,请重新选择!");
                            txt_BillNo.Text = "";
                            return;
                        }
                        else
                        {
                            lblInNum.Text = DtIn.Rows[0]["PlanQty"].ToString();
                            productNumber = DtIn.Rows[0]["PartNumber"].ToString();

                            lab_msg.Text = GetBillInfoMsgBegin(txt_BillNo.Text, lblRow.Text, lblOrder.Text, productNumber);
                        }
                    }
                    else
                    {
                        MessageBox.Show("没有入库信息!");
                        txt_BillNo.Text = "";
                        return;
                    }

                    // lab_msg.Text = BillNo + "当前料号:" + productNumber;
                    txtSN.Focus();
                }
                catch
                {
                    txt_BillNo.Text = "";
                    txt_BillNo.Focus();
                }
            }
        }

        private void FrmRMIn_Load(object sender, EventArgs e)
        {

            txt_BillNo.Focus();
        }

        private void txtSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {

                string standar = string.Empty;  //库位规格
                decimal qty = 0;      //数量
                string billno = string.Empty;//单号
                string rowno = string.Empty; //行号
                string orderno = string.Empty; //序号
                string Storage_SN = string.Empty; //仓库
                string Area_SN = string.Empty;    //库区
                string Location_SN = string.Empty;//库位
                string version = string.Empty;     //版本
                string datecode = string.Empty;
                string lotno = string.Empty;
                string PartNumber = string.Empty; //物料号
                string RelatedBill = string.Empty;//ERP单号

                try
                {

                    if (DtIn != null && DtIn.Rows.Count > 0)
                    {
                        standar = DtIn.Rows[0]["Area_Standard"].ToString();
                        qty = Convert.ToDecimal(DtIn.Rows[0]["PlanQty"]);
                        billno = DtIn.Rows[0]["BillNo"].ToString();
                        rowno = DtIn.Rows[0]["RowNo"].ToString();
                        orderno = DtIn.Rows[0]["OrderNo"].ToString();
                        version = DtIn.Rows[0]["Version"].ToString();
                        datecode = DtIn.Rows[0]["DateCode"].ToString();
                        lotno = DtIn.Rows[0]["SupLotNo"].ToString();
                        PartNumber = DtIn.Rows[0]["PartNumber"].ToString();
                        RelatedBill = DtIn.Rows[0]["RelatedBill"].ToString();
                    }

                    Location_SN = txtSN.Text;

                    try
                    {
                        var snArray = Location_SN.Split('#');

                        Storage_SN = snArray[0];
                        Area_SN = snArray[0] + '#' + snArray[1];
                        var storageSN = DtRMIn.Rows[0]["Storage_SN"].ToString();
                        if (Storage_SN.Equals(storageSN) == false)
                        {
                            MessageBox.Show("入库仓库" + Storage_SN + "与订单指定仓库" + storageSN + "不一致！");
                            return;
                        }

                        txtSN.Text = snArray[1] + snArray[2];
                    }
                    catch
                    {
                        MessageBox.Show("库位" + Location_SN + "格式不正确，请重新输入!");
                        txtSN.Text = "";
                        return;
                    }

                    var locDt = Bll_RMIn.QueryLocation(Location_SN);
                    if (locDt.Rows.Count == 0)
                    {
                        MessageBox.Show(Location_SN + "库位不存在，请重新输入!");
                        txtSN.Text = "";
                        return;
                    }

                    if (Bll_RMIn.IsStandard(PartNumber, Location_SN) == false)
                    {
                        MessageBox.Show("库位规格不符!");
                        return;
                    }

                    var InveDt = Bll_RMIn.GetInventory(Location_SN, PartNumber);
                    bool inveBool = false;
                    bool inveNEW = true;
                    if (InveDt != null && InveDt.Rows.Count > 0)
                    {
                        if ((datecode != InveDt.Rows[0]["DateCode"].ToString()) || (version != InveDt.Rows[0]["version"].ToString()))
                        {
                            inveBool = true;
                        }
                        else
                        {
                            inveNEW = false;
                        }
                    }
                    if (inveBool)
                    {
                        MessageBox.Show("该库位物DateCode或版本与入库产品不符!");
                        return;
                    }

                    MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

                    DialogResult dr = MessageBox.Show("库位[" + Location_SN + "]，是否确认？", "出库确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

                    if (dr == DialogResult.Cancel)
                    {
                        return;
                    }

                    DtRMIn = Bll_RMIn.GetRMTL(billno);
                    if (DtRMIn.Rows.Count == 0)
                    {
                        MessageBox.Show("该订单已被操作上架，请确认!");
                        return;
                    }

                    bool inStock = Bll_RMIn.ProductInStockPRRM(qty, billno, rowno, orderno, Storage_SN, Area_SN, Location_SN, version, datecode, lotno, PartNumber, RelatedBill, DetailNo, inveNEW);

                    if (inStock)
                    {
                        lab_msg.Text = txt_BillNo.Text + "入库成功！" + GetBillInfoMsg(billno, rowno, orderno, PartNumber);
                        txt_BillNo.Focus();
                        txt_BillNo.Text = "";
                        txtSN.Text = "";
                    }
                    else
                    {
                        lab_msg.Text = txt_BillNo.Text + "入库失败!";
                        txt_BillNo.Focus();
                        txt_BillNo.Text = "";
                        txtSN.Text = "";
                    }

                }
                catch
                {
                    lab_msg.Text = txt_BillNo.Text + "程序错误!";
                    txt_BillNo.Focus();
                    txt_BillNo.Text = "";
                    txtSN.Text = "";
                }
            }
        }


        public string GetBillInfoMsg(string billNo, string rowno, string orderno, string PartNumber)
        {
            try
            {
                var dt = Bll_RMIn.GetISRMD(billNo, rowno, orderno);
                StringBuilder sb = new StringBuilder();
                if (dt != null && dt.Rows.Count > 0)
                {
                    var rmDetailCount = dt.Rows.Count;
                    int rmDetailOverCount = 0;
                    decimal planQtyCount = 0;
                    decimal realQtyCount = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        planQtyCount = planQtyCount + Convert.ToDecimal(dr["PlanQty"]);
                        if (dr["Status"].ToString() == "9")
                        {
                            rmDetailOverCount = rmDetailOverCount + 1;
                            realQtyCount = realQtyCount + Convert.ToDecimal(dr["RealQty"]);
                        }
                    }

                    var isFinish = false;

                    sb.AppendLine("料号:" + PartNumber + "\r\n上架托数:" + rmDetailOverCount + "/" + rmDetailCount + "\r\n上架数量:" + realQtyCount + "/" + planQtyCount);
                    if (rmDetailOverCount == rmDetailCount) //如果所有托数已经完成，修改行表状态为完成
                    {
                        isFinish = true;
                        var resRow = Bll_RMIn.UpdateRMRow(billNo, rowno, orderno);
                        if (resRow)
                        {
                            var rowDate = Bll_RMIn.GetRMRow(billNo);
                            if (!((rowDate != null) && (rowDate.Rows.Count > 0))) //每当有行表状态为完成时，判断是否该订单所有行数据已经完成，如果完成，则修改该订单状态为完成 并提示该订单完成。
                            {
                                var res = Bll_RMIn.UpdateRMPR(billNo);
                                if (res)
                                {
                                    MessageBox.Show("该订单" + billNo + "已完成!");
                                }
                            }
                        }

                    }


                }

                return sb.ToString();
            }
            catch
            {
                return "";
            }
        }


        public string GetBillInfoMsgBegin(string billNo, string rowno, string orderno, string PartNumber)
        {
            try
            {
                var dt = Bll_RMIn.GetISRMD(billNo, rowno, orderno);
                StringBuilder sb = new StringBuilder();
                if (dt != null && dt.Rows.Count > 0)
                {
                    var rmDetailCount = dt.Rows.Count;
                    int rmDetailOverCount = 0;
                    decimal planQtyCount = 0;
                    decimal realQtyCount = 0;
                    foreach (DataRow dr in dt.Rows)
                    {
                        planQtyCount = planQtyCount + Convert.ToDecimal(dr["PlanQty"]);
                        if (dr["Status"].ToString() == "9")
                        {
                            rmDetailOverCount = rmDetailOverCount + 1;
                            realQtyCount = realQtyCount + Convert.ToDecimal(dr["RealQty"]);
                        }
                    }

                    var isFinish = false;

                    sb.AppendLine("料号:" + PartNumber + "\r\n上架托数:" + rmDetailOverCount + "/" + rmDetailCount + "\r\n上架数量:" + realQtyCount + "/" + planQtyCount);
                }

                return sb.ToString();
            }
            catch
            {
                return "";
            }
        }

    }
}